﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using H2.NET.Controls;
using H2.NET.Data.Common;
using H2.NET.Business;

namespace H2.NET.Web.Pages
{
    public partial class TabInsQuan : TaskControl
    {
        #region 属性 Public
        public MDCTabStatus Status
        {
            get
            {
                if (StoreAPI["Status~" + Key] == null)
                {
                    StoreAPI["Status~" + Key] = new MDCTabStatus();
                }

                return (MDCTabStatus)StoreAPI["Status~" + Key];
            }
            set
            {
                Key = value.KeyWord;
                StoreAPI["Status~" + Key] = value;
            }
        }

        public string Key
        {
            get
            {
                return ViewState["Key"] == null ? "" : (string)ViewState["Key"];

            }
            set { ViewState["Key"] = value; }
        }

        public int SheetIndex
        {
            get
            {
                return ViewState["SheetIndex"] == null ? 0 : (int)ViewState["SheetIndex"];

            }
            set { ViewState["SheetIndex"] = value; }
        }
        #endregion

        #region 事件 Load (页面)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
       protected void Page_Load(object sender, EventArgs e)
        {
            InitDetails();

            if (!Page.IsPostBack)
            {
                InitControls();
            }
         }

         public void InitControls()
        {
            if (Status.Category == "INSP.QUAN")
            {
                IList<mdc_Ins_Quan> ins_qual = (List<mdc_Ins_Quan>)gv_Ins_Quan.DataSource;

                if (!Page.IsPostBack)
                {
                    gv_Ins_Quan.Key = Key;
                    Quan_P12.Visible = true;

                    #region 发起步骤
                    if (TaskAPI.IsInitiate)
                    {
                        List<mdc_Ins_Quan> ins_quan_s = new List<mdc_Ins_Quan>();
                        for (int i = 0; i < 4; i++)
                        {
                            ins_quan_s.Add(new mdc_Ins_Quan());
                            ins_quan_s[i].No = i + 1;
                            if (TaskAPI.Process == "TW Inspection Plan Maintenance")
                            {
                                ins_quan_s[i].InfoField_3 = "0";
                                ins_quan_s[i].Search_Field = "0";
                                ins_quan_s[i].Plant = "";
                            }
                        }
                        gv_Ins_Quan.DataSource = ins_quan_s;
                    }
                    #endregion

                    #region 审批环节
                    else
                    {
                        IList<mdc_Ins_Quan> ins_quan_s = (mdc_Ins_Quan.process == TaskAPI.Process & mdc_Ins_Quan.incident == TaskAPI.Incident).List<mdc_Ins_Quan>();
                        if (ins_quan_s.Count == 0)
                        {
                            for (int i = 0; i < 4; i++)
                            {
                                ins_quan_s.Add(new mdc_Ins_Quan());
                                ins_quan_s[i].No = i + 1;
                                if (TaskAPI.Process == "TW Inspection Plan Maintenance")
                                {
                                    ins_quan_s[i].InfoField_3 = "0";
                                    ins_quan_s[i].Search_Field = "0";
                                    ins_quan_s[i].Plant = "";
                                }
                            }
                        }
                        gv_Ins_Quan.DataSource = ins_quan_s;

                    }
                    #endregion
                }
            }
        }

        #endregion

        #region 事件 Add(增加行)
        protected void Add_Click(object sender, EventArgs e)
        {
            switch (((Anthem.Button)sender).ID)
            {
                case "Ins_Quan_Add":
                    List<mdc_Ins_Quan> ins_quan_s = (List<mdc_Ins_Quan>)gv_Ins_Quan.DataSource;
                    mdc_Ins_Quan ins_quan = new mdc_Ins_Quan();
                    ins_quan.No = ins_quan_s.Count + 1;
                    if (TaskAPI.Process == "TW Inspection Plan Maintenance")
                    {
                        ins_quan.InfoField_3 = "0";
                        ins_quan.Search_Field = "0";
                        ins_quan.Plant = "";
                    }
                    ins_quan_s.Insert(gv_Ins_Quan.ChangedRowIndex == -1 || gv_Ins_Quan.ChangedRowIndex >= ins_quan_s.Count ? ins_quan_s.Count - 1 : gv_Ins_Quan.ChangedRowIndex, ins_quan);
                    gv_Ins_Quan.DataSource = ins_quan_s;
                    gv_Ins_Quan.DataBind();
                    break;
            }
        }
        #endregion

        #region 方法 InitDetails(初始化明细行)
            public void InitDetails()
            {
                bool flg = Status.Technical == "C" && TaskAPI.IsAllowSubmit;
                if (TaskAPI.Process == "TW Inspection Plan Maintenance")
                {
                    SetVisible(Status.Technical == "C", Control_Indicators_L, Control_Indicators, Preset_indicators_L, Preset_indicators, Status1_L, Status1);
                    gv_Ins_Quan.DynamicColumn.Add(new BoundColumn2("00", "No", BoundColumnType2.TextBox, 0, Align.Left, true, "No", InputType.None, null));
                    gv_Ins_Quan.DynamicColumn.Add(new BoundColumn2("01", "Plant", BoundColumnType2.DropDownList, 60, Align.Left, flg, "Plant", InputType.None, new string[3] { "", "KT1P", "KT2P"}));
                    gv_Ins_Quan.DynamicColumn.Add(new BoundColumn2("02", "Old Name", BoundColumnType2.TextBox, 150, Align.Left, flg, "Valid_From", InputType.None, null));
                    gv_Ins_Quan.DynamicColumn.Add(new BoundColumn2("03", "SAP Characteristic Code", BoundColumnType2.TextBox, 200, Align.Left, flg, "Characteristic_Code", InputType.Numeric, null));
                    gv_Ins_Quan.DynamicColumn.Add(new BoundColumn2("04", "Type", BoundColumnType2.DropDownList, 100, Align.Left, flg, "Type", InputType.None, new string[4] { "", "Creation", "Revision", "Deletion" }));
                    gv_Ins_Quan.DynamicColumn.Add(new BoundColumn2("05", "Short text", BoundColumnType2.TextBox, 150, Align.Left, flg, "Short_Text", InputType.None, null));
                    gv_Ins_Quan.DynamicColumn.Add(new BoundColumn2("06", "Description(ZF)", BoundColumnType2.TextBox, 200, Align.Left, flg, "Description_ZH", InputType.None, null));
                    gv_Ins_Quan.DynamicColumn.Add(new BoundColumn2("07", "Search field", BoundColumnType2.TextBox, 150, Align.Left, flg, "Search_Field", InputType.None, null));
                    gv_Ins_Quan.DynamicColumn.Add(new BoundColumn2("08", "InfoField 3", BoundColumnType2.TextBox, 200, Align.Left, flg, "InfoField_3", InputType.None, null));
                    gv_Ins_Quan.DynamicColumn.Add(new BoundColumn2("09", "Decimal places", BoundColumnType2.TextBox, 150, Align.Left, flg, "Decimal_Places", InputType.Numeric, null));
                    gv_Ins_Quan.DynamicColumn.Add(new BoundColumn2("10", "Msmt unit", BoundColumnType2.TextBox, 100, Align.Left, flg, "Msmt_Unit", InputType.None, null));
                    gv_Ins_Quan.DynamicColumn.Add(new BoundColumn2("11", "Lower spec.lmt", BoundColumnType2.TextBox, 100, Align.Left, flg, "Lower_Spec_Lmt", InputType.Numeric, null));
                    gv_Ins_Quan.DynamicColumn.Add(new BoundColumn2("12", "Upper Limit", BoundColumnType2.TextBox, 100, Align.Left, flg, "Upper_Limit", InputType.Numeric, null));
                    //gv_Ins_Quan.DynamicColumn.Add(new BoundColumn2("13", "Target Value", BoundColumnType2.TextBox, 100, Align.Left, flg, "Target_Value", InputType.Numeric, null));
                    //gv_Ins_Quan.DynamicColumn.Add(new BoundColumn2("14", "Status", BoundColumnType2.DropDownList, 100, Align.Left, false, "InsQuan_Status", InputType.None, new string[6] { "", "1, Being Created", "2, Released", "3, Can no longer be", "4, Deletion flag", "5, Archived" }));

                }
                else
                {
                    gv_Ins_Quan.DynamicColumn.Add(new BoundColumn2("00", "No", BoundColumnType2.TextBox, 0, Align.Left, true, "No", InputType.None, null));
                    gv_Ins_Quan.DynamicColumn.Add(new BoundColumn2("01", "Plant", BoundColumnType2.DropDownList, 60, Align.Left, false, "Plant", InputType.None, null));
                    gv_Ins_Quan.DynamicColumn.Add(new BoundColumn2("02", "Valid From</br>(默认为DMO创建日期)", BoundColumnType2.Calender, 150, Align.Left, flg, "Valid_From", InputType.None, null));
                    gv_Ins_Quan.DynamicColumn.Add(new BoundColumn2("03", "Characteristic Code", BoundColumnType2.TextBox, 200, Align.Left, flg, "Characteristic_Code", InputType.Numeric, null));
                    gv_Ins_Quan.DynamicColumn.Add(new BoundColumn2("04", "Type", BoundColumnType2.DropDownList, 100, Align.Left, flg, "Type", InputType.None, new string[4] { "", "Creation", "Revision", "Deletion" }));
                    gv_Ins_Quan.DynamicColumn.Add(new BoundColumn2("05", "Short text", BoundColumnType2.TextBox, 150, Align.Left, flg, "Short_Text", InputType.None, null));
                    gv_Ins_Quan.DynamicColumn.Add(new BoundColumn2("06", "Description(ZH)", BoundColumnType2.TextBox, 200, Align.Left, flg, "Description_ZH", InputType.None, null));
                    gv_Ins_Quan.DynamicColumn.Add(new BoundColumn2("07", "Search field", BoundColumnType2.TextBox, 150, Align.Left, flg, "Search_Field", InputType.None, null));
                    gv_Ins_Quan.DynamicColumn.Add(new BoundColumn2("08", "InfoField 3", BoundColumnType2.TextBox, 200, Align.Left, flg, "InfoField_3", InputType.None, null));
                    gv_Ins_Quan.DynamicColumn.Add(new BoundColumn2("09", "Decimal places", BoundColumnType2.TextBox, 150, Align.Left, flg, "Decimal_Places", InputType.Numeric, null));
                    gv_Ins_Quan.DynamicColumn.Add(new BoundColumn2("10", "Msmt unit", BoundColumnType2.TextBox, 100, Align.Left, flg, "Msmt_Unit", InputType.None, null));
                    gv_Ins_Quan.DynamicColumn.Add(new BoundColumn2("11", "Lower spec.lmt", BoundColumnType2.TextBox, 100, Align.Left, flg, "Lower_Spec_Lmt", InputType.Numeric, null));
                    gv_Ins_Quan.DynamicColumn.Add(new BoundColumn2("12", "Upper Limit", BoundColumnType2.TextBox, 100, Align.Left, flg, "Upper_Limit", InputType.Numeric, null));
                    gv_Ins_Quan.DynamicColumn.Add(new BoundColumn2("13", "Target Value", BoundColumnType2.TextBox, 100, Align.Left, flg, "Target_Value", InputType.Numeric, null));
                    gv_Ins_Quan.DynamicColumn.Add(new BoundColumn2("14", "Status", BoundColumnType2.DropDownList, 100, Align.Left, false, "InsQuan_Status", InputType.None, new string[6] { "", "1, Being Created", "2, Released", "3, Can no longer be", "4, Deletion flag", "5, Archived" }));
                }
                gv_Ins_Quan.Width = 1000;
                SetEnable(flg, Ins_Quan_Add);
                SetEnable(flg, Ins_Quan_Delete);
            }
            #endregion

        #region 事件 Delete(删除行)
            protected void Delete_Click(object sender, EventArgs e)
            {
                switch (((Anthem.Button)sender).ID)
                {
                    case "Ins_Quan_Delete":
                        List<mdc_Ins_Quan> ins_quan_s = (List<mdc_Ins_Quan>)gv_Ins_Quan.DataSource;
                        IList<mdc_Ins_Quan> list = (mdc_Ins_Quan.process == TaskAPI.Process & mdc_Ins_Quan.incident == TaskAPI.Incident).List<mdc_Ins_Quan>();
                        if (ins_quan_s.Count == 1)
                        {
                            Anthem.Manager.AddScriptForClientSideEval("JSInfomation('至少保留最后一个 Sheet ！',false)");
                            return;
                        }
                        ins_quan_s.RemoveAt(gv_Ins_Quan.ChangedRowIndex == -1 || gv_Ins_Quan.ChangedRowIndex >= ins_quan_s.Count ? ins_quan_s.Count - 1 : gv_Ins_Quan.ChangedRowIndex);
                        bool flag = false;
                        foreach (mdc_Ins_Quan ins_quan_1 in list)
                        {
                            if (string.IsNullOrEmpty(ins_quan_1.Characteristic_Code))
                            {
                                continue;
                            }
                            foreach (mdc_Ins_Quan ins_quan_2 in ins_quan_s)
                            {

                                if (string.IsNullOrEmpty(ins_quan_1.Characteristic_Code))
                                {
                                    continue;
                                }
                                if (ins_quan_2.ID == ins_quan_1.ID)
                                {
                                    flag = true;
                                    break;
                                }
                            }
                            if (!flag)
                            {
                                Storage.Delete(ins_quan_1);
                            }
                            flag = false;
                        }

                        gv_Ins_Quan.DataSource = ins_quan_s;
                        gv_Ins_Quan.DataBind();
                        break;
                }
            }
          #endregion

        #region 方法 Save(保存数据库)
            public void Save()
            {
                if (!Status.IsEdited)
                {
                    return;
                }

                switch (Status.Category)
                {

                    #region QUAN
                    case "INSP.QUAN":
                        foreach (mdc_Ins_Quan ins_quan in (List<mdc_Ins_Quan>)gv_Ins_Quan.DataSource)
                        {
                            if (string.IsNullOrEmpty(ins_quan.Characteristic_Code))
                            {
                                continue;
                            }
                            ins_quan.Process = TaskAPI.Process;
                            ins_quan.Incident = TaskAPI.Incident;
                            ins_quan.UserName = UserAPI.UserName;
                            ins_quan.Time = System.DateTime.Now;
                            ins_quan.Status = 0;
                            Storage.Save(ins_quan);
                        }
                        break;
                    #endregion
                }
            }
           #endregion

        #region 方法 DataValidation(数据验证)
            public List<string> DataValidation()
            {
                //如果当前 Tabs 是隐藏的情况，退出
                //if (!Status.IsEdited)
                //{
                //    return new List<string>();
                //}

                List<string> vald_s = new List<string>();
                List<Regular> regs = new List<Regular>();
                if (TaskAPI.Process == "TW Inspection Plan Maintenance")
                {
                    if ((Page as PMDC).Quan_Selected.Checked && (Page as PMDC).Quan_Selected.Visible)
                    {
                        foreach (mdc_Ins_Quan ccc in (List<mdc_Ins_Quan>)this.gv_Ins_Quan.DataSource)
                        {
                            if (string.IsNullOrEmpty(ccc.Plant) && string.IsNullOrEmpty(ccc.Characteristic_Code) && string.IsNullOrEmpty(ccc.Type) && string.IsNullOrEmpty(ccc.Short_Text) && string.IsNullOrEmpty(ccc.Description_ZH) && string.IsNullOrEmpty(ccc.Lower_Spec_Lmt) && string.IsNullOrEmpty(ccc.Upper_Limit))
                            {
                                continue;
                            }
                            regs.Add(new Regular(RegularExp.NotEmpty, ccc.Plant, "&nbsp;&nbsp;  表单 行" + ccc.No + ": Plant为必填"));
                            regs.Add(new Regular(RegularExp.NotEmpty, ccc.Characteristic_Code, "&nbsp;&nbsp;  表单 行" + ccc.No + ": SAP Characteristic Code为必填"));
                            regs.Add(new Regular(RegularExp.NotEmpty, ccc.Type, "&nbsp;&nbsp;  表单 行" + ccc.No + ": Type为必填"));
                            regs.Add(new Regular(RegularExp.NotEmpty, ccc.Short_Text, "&nbsp;&nbsp;  表单 行" + ccc.No + ": Short text为必填"));
                            regs.Add(new Regular(RegularExp.NotEmpty, ccc.Description_ZH, "&nbsp;&nbsp;  表单 行" + ccc.No + ": Description(ZF)为必填"));
                            regs.Add(new Regular(RegularExp.NotEmpty, ccc.Lower_Spec_Lmt, "&nbsp;&nbsp;  表单 行" + ccc.No + ": Lower spec.lmt为必填"));
                            regs.Add(new Regular(RegularExp.NotEmpty, ccc.Upper_Limit, "&nbsp;&nbsp;  表单 行" + ccc.No + ": Upper Limit 为必填"));
                        }
                    }
                }
                vald_s.AddRange(Validation(regs));

                if (vald_s.Count > 0)
                {
                    vald_s.Insert(0, "<font color='#0000FF'><b>" + Status.Name + "</b></font>");
                    vald_s.Add("&nbsp;&nbsp;-----------------------------------------");
                }

                return vald_s;
            }
            #endregion

        #region 方法 gv_InsQuan 数据取得
            protected void gv_Ins_Quan_DataBinding(object sender, EventArgs e)
            {
                if (gv_Ins_Quan.ChangedColumnIndex == 4 || gv_Ins_Quan.ChangedColumnIndex == 5 || gv_Ins_Quan.ChangedColumnIndex == 6 || gv_Ins_Quan.ChangedColumnIndex == 7)
                {
                    List<mdc_Ins_Quan> ins_quan_s = gv_Ins_Quan.DataSource as List<mdc_Ins_Quan>;
                    //if (gv_Ins_Qual.ChangedRowIndex >= 0 && gv_Ins_Qual.ChangedRowIndex < ins_qual_s.Count)
                    //{
                    ins_quan_s[gv_Ins_Quan.ChangedRowIndex].Search_Field = ins_quan_s[gv_Ins_Quan.ChangedRowIndex].Short_Text;
                    ins_quan_s[gv_Ins_Quan.ChangedRowIndex].InfoField_3 = ins_quan_s[gv_Ins_Quan.ChangedRowIndex].Short_Text;
                    //}

                    gv_Ins_Quan.DataSource = ins_quan_s;
                }
            }
            #endregion
    }
}
